package com.hermes.comics;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.text.Html;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;

import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;

import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.TextView;

import com.hermes.comics.R;
import com.hermes.comics.data.Constants;
import com.hermes.comics.data.Catalog;
import com.hermes.comics.data.Issue;
import com.hermes.comics.data.IssueTag;
import com.hermes.comics.data.Serie;
import com.hermes.comics.data.SerieTag;
import com.hermes.comics.dialogs.AutoUpdateIssueDialog;
import com.hermes.comics.dialogs.SaveItemDialog;
import com.hermes.comics.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

public class WebActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web_activity);

    Catalog.init(this);

    Serie serie = Catalog.getInstance().getSerie(getSerieKey());
    Issue issue = serie.getIssue(getIssueKey());

    getActionBar().setDisplayShowCustomEnabled(true);
    getActionBar().setDisplayShowTitleEnabled(true);
    getActionBar().setDisplayUseLogoEnabled(true);
    getActionBar().setTitle(SerieTag.TITLE.value(serie));
    getActionBar().setSubtitle("Issue: " + IssueTag.KEY.value(issue));

    String url = IssueTag.URL.value(issue);

    WebView webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);

    webView.setWebChromeClient(new WebChromeClient() {
      @Override
      public void onProgressChanged(WebView view, int progress) {
        // Activities and WebViews measure progress with different scales.
        // The progress meter will automatically disappear when we reach 100%
        WebActivity.this.setProgress(progress * 1000);
      }
    });
    webView.setWebViewClient(new WebViewClient() {
      @Override
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Toast.makeText(WebActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
      }
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
        System.out.println("----------------" + url);
        return true;
      }

      @Override
      public WebResourceResponse shouldInterceptRequest(WebView view, String url)  {
        if (isImage(url)) {
          System.out.println("--------- shouldInterceptRequest -------" + url);
        }
        return super.shouldInterceptRequest(view, url);
      }      
    });

    webView.loadUrl(url);
  }

  private boolean isImage(String url) {
    url = url.toLowerCase();
    return (url.endsWith("jpg") || url.endsWith("png") || url.endsWith("gif") || url.endsWith("jpeg"));
  }

  private String getSerieKey() {
    return getIntent().getStringExtra(Constants.SERIE_KEY);
  }

  private String getIssueKey() {
    return getIntent().getStringExtra(Constants.ISSUE_KEY);
  }
}
